package com.chinameyer.equipment_ebook.controller;

import com.chinameyer.equipment_ebook.common.result.ApiResponse;
import com.chinameyer.equipment_ebook.service.UserService;
import com.chinameyer.equipment_ebook.vo.LoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author meyer@HongYe
 */
@RestController
@RequestMapping("/v1/user/")
@Validated
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 用户登录
     * @param username 用户名
     * @param password 密码
     * @return 登录响应结果
     */
    @PostMapping("login")
    public ApiResponse login(@RequestParam(name = "username") String username,
                             @RequestParam(name = "password") String password){
        boolean isExistUsername = this.userService.checkUsername(username);
        if(!isExistUsername){
            return new ApiResponse(400,"用户名不存在",0);
        }
        LoginVO loginVO = this.userService.verifyAccount(username,password);
        if(StringUtils.isEmpty(loginVO)){
            return new ApiResponse(400,"密码输入错误",0);
        }
        return new ApiResponse(200,"success",loginVO);
    }


    /**
     * 修改用户密码
     * @param username        用户名
     * @param oldPassword     旧密码
     * @param newPassword     新密码
     * @param confirmPassword 确认密码
     * @return 修改密码后响应
     */
    @PostMapping("changePassword")
    public ApiResponse changePassword(@RequestParam(name = "username") String username,
                                      @RequestParam(name = "oldPassword") String oldPassword,
                                      @RequestParam(name = "newPassword") String newPassword,
                                      @RequestParam(name = "confirmPassword") String confirmPassword){
        LoginVO loginVO = this.userService.verifyAccount(username,oldPassword);
        if(StringUtils.isEmpty(loginVO)){
            return new ApiResponse(400,"旧密码输入错误，请重新输入",0);
        }
        if(!newPassword.equalsIgnoreCase(confirmPassword)){
            return new ApiResponse(400,"输入的新密码不一致，请重新输入",0);
        }
        boolean isUpdateSuccess = this.userService.updatePassword(loginVO,newPassword);
        if(!isUpdateSuccess){
            return new ApiResponse(400,"密码更新失败",0);
        }
        return new ApiResponse(200,"success",1);
    }


    /**
     * 下载同一机器的所有说明书二维码
     * @param equipmentNo 设备编码
     * @return 下载结果响应
     */
    @GetMapping("downloadQRCode")
    public ApiResponse downloadQRCode(@RequestParam(name = "equipmentNo") String equipmentNo){
        try {
            String url = this.userService.downloadQRCode(equipmentNo);
            return new ApiResponse(200,"success",url);
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResponse(400,"二维码下载失败！",0);
        }
    }


    /**
     * 获取说明书 URL
     * @param eventId     事件ID
     * @return 获取结果响应
     */
    @GetMapping("getManual/{eventId}")
    public ApiResponse getManual(HttpServletResponse response,@PathVariable("eventId") int eventId){
        this.userService.getManual(eventId,response);
        return new ApiResponse(200,"success",1);
    }
}
